package com.drugStorage.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.drugStorage.domain.User;
import com.drugStorage.hibernate.Utils.HibernateUtils;
import com.drugStorage.hibernate.dao.UserDao;

public class UserDaoImpl implements UserDao {

	public void removeUser(User user) {

		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.delete(user);
			tx.commit();
		} finally {
			if (s != null)
				s.close();

		}

	}

	public void saveUser(User user) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.save(user);
			tx.commit();

		} finally {
			if (s != null)
				s.close();
		}

	}

	public void updateUser(User user) {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();

			session.saveOrUpdate(user);
			tx.commit();

		} finally {
			if (session != null)
				session.close();

		}
	}

	public User getUser(String id) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			Criteria c = s.createCriteria(User.class);
			c.add(Restrictions.eq("id", id));
			User user = (User) c.uniqueResult();
			return user;

		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			if (s != null)
				s.close();
		}

	}

	public void delObject(String[] userList) {
		Session session = null;
		Transaction tx = null;
		StringBuffer sbfSql = new StringBuffer();

		for (int i = 0; i < userList.length; i++) {
			sbfSql.append("'").append(userList[i]).append("'").append(",");
		}
		String sql = "delete from User where name in ("
				+ sbfSql.substring(0, sbfSql.length() - 1) + ")";

		// System.out.println("ɾ���sql����ǣ�" + sql);
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();

			// session.createQuery(sql);
			session.createSQLQuery(sql).executeUpdate();
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List findByUsername(User user) {
		Session s = null;
		Transaction tx = null;

		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			String hql = "from User user ";
			List userList = s.createQuery(hql).list();
			return userList;

		} catch (HibernateException e) {
			e.printStackTrace();
			throw e;
		}finally{
			if(s!=null)
				s.close();
		}
	}

	public User getByName(String name) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			Criteria c = s.createCriteria(User.class);
			c.add(Restrictions.eq("name", name));
			User user = (User) c.uniqueResult();
			return user;

		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			if (s != null)
				s.close();
		}
	}

}
